//
//  XVCMessage.m  展商消息
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/11.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCMessage.h"
#import "XXTool.h"
#import "XVVNewUserSeeButton.h"
#import "XTCAppointmentInformationShow.h"
#import "XVCAppointmentRecord.h"
#import "XVCSomeoneSeenU.h"
#import "XVCAppontmentDetail.h"
#import "XVCRefuseVisitor.h"
#import "VisitorMessageLiuyanCell.h"
#import "LiuyanViewController.h"

@interface XVCMessage ()<UITableViewDelegate,UITableViewDataSource,XTCAppointmentInformationShowDelegate>

@property (nonatomic,strong) UITableView *tableview;

@property (nonatomic,strong) XVVNewUserSeeButton *showSeeUser;

@property (nonatomic,strong) UIView *navigationTopView;

@property(nonatomic,strong) UIButton *message;

@property(nonatomic,strong) UIButton *YuyueMessage;

@property(nonatomic,strong) CALayer * Liuyantemp;

@property(nonatomic,strong) CALayer * temp;

@property(nonatomic) Boolean isYuyueMessage;

@end

@implementation XVCMessage
{
    NSMutableArray <XMLMessageModel*> *useData;
    BOOL isHaveLookedUser;
}

-(UIView*)navigationTopView{
    
    
    
    if(_navigationTopView == nil){
        _navigationTopView = [[UIView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-140)/2
                                                                      , (44-30)/2
                                                                      , 140
                                                                      , 30)];
        
        _message=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, _navigationTopView.frame.size.width/2, _navigationTopView.frame.size.height)];
        _Liuyantemp = [CALayer layer];
        _Liuyantemp.frame = CGRectMake(1, 1, _message.bounds.size.width - 1, _message.bounds.size.height-2);
        [_Liuyantemp setBackgroundColor:[XXTool colorWithProject].CGColor];
        
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:_Liuyantemp.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(5.5, 5.5)];
        
        CAShapeLayer * mask  = [[CAShapeLayer alloc] initWithLayer:_Liuyantemp];
        mask.path = path.CGPath;
        _Liuyantemp.mask = mask;
        [_message.layer addSublayer:_Liuyantemp];
        
        UIBezierPath * path2 = [UIBezierPath bezierPathWithRoundedRect:_message.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(6.0, 6.0)];
        CAShapeLayer * mask2  = [[CAShapeLayer alloc] initWithLayer:_Liuyantemp];
        mask2.path = path2.CGPath;
        _message.layer.mask = mask2;
        _message.backgroundColor=[XXTool colorWithProject];
        [_message setTitle:NSLocalizedString(@"消息", nil) forState:UIControlStateNormal];
        [_message setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _message.titleLabel.font = [XXTool systemFontOfSize:15];
        [_message addTarget:self action:@selector(clickMessage) forControlEvents:UIControlEventTouchDown];
        [_navigationTopView addSubview:_message];
        
        
        _YuyueMessage=[[UIButton alloc]initWithFrame:CGRectMake(_message.frame.size.width, 0, _navigationTopView.frame.size.width/2, _navigationTopView.frame.size.height)];
        
        _temp = [CALayer layer];
        _temp.frame = CGRectMake(0, 1, _YuyueMessage.bounds.size.width - 1, _YuyueMessage.bounds.size.height-2);
        [_temp setBackgroundColor:[UIColor whiteColor].CGColor];
        
        UIBezierPath * path1 = [UIBezierPath bezierPathWithRoundedRect:_temp.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(5.5, 5.5)];
        
        CAShapeLayer * mask1  = [[CAShapeLayer alloc] initWithLayer:_temp];
        mask1.path = path1.CGPath;
        _temp.mask = mask1;
        [_YuyueMessage.layer addSublayer:_temp];
        
        UIBezierPath * path3 = [UIBezierPath bezierPathWithRoundedRect:_YuyueMessage.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(6.0, 6.0)];
        CAShapeLayer * mask3  = [[CAShapeLayer alloc] initWithLayer:_temp];
        mask3.path = path3.CGPath;
        _YuyueMessage.layer.mask = mask3;
        
        _YuyueMessage.backgroundColor=[XXTool colorWithProject];
        [_YuyueMessage setTitle:NSLocalizedString(@"预约", nil) forState:UIControlStateNormal];
        [_YuyueMessage setTitleColor:[XXTool colorWithProject] forState:UIControlStateNormal];
        _YuyueMessage.titleLabel.font=[XXTool systemFontOfSize:15];
        [_YuyueMessage addTarget:self action:@selector(clickYuyueMessage) forControlEvents:UIControlEventTouchDown];
        [_navigationTopView addSubview:_YuyueMessage];
    }
    return  _navigationTopView;
}


-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = [XXTool colorWithBackGround];
        MJRefreshNormalHeader *useHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        [useHead setTitle:NSLocalizedString(@"下拉刷新", nil) forState:MJRefreshStateIdle];
        [useHead setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useHead setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_header = useHead;
        
        MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(XVVNewUserSeeButton*)showSeeUser
{
    if (_showSeeUser==nil)
    {
        _showSeeUser = [[XVVNewUserSeeButton alloc] init];
        _showSeeUser.userInteractionEnabled = YES;
        [_showSeeUser addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickShowSeenU)]];
        [self.view addSubview:_showSeeUser];
    }
    return _showSeeUser;
}

-(void)initParameter
{
    useData = [NSMutableArray array];
    isHaveLookedUser = NO;
}

-(void)initUI
{
    //[self.navigationItem setTitle:NSLocalizedString(@"消息", nil)];
    
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"更多"] style:UIBarButtonItemStylePlain target:self action:@selector(clickleft)]];
    
//    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"预约记录", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clickApponintmentRecord)]];
    _isYuyueMessage=false;
    
    [self.navigationItem setTitleView:self.navigationTopView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickCellLiuyan:) name:@"ExhiLiuyan" object:nil];
    
}

-(void)clickCellLiuyan:(NSNotification *)notification{
    
    NSString *tagg=[NSString stringWithFormat:@"%ld",[[notification.userInfo objectForKey:@"tag"] integerValue]];
    [LiuyanViewController pushVistorLiuyanDetailWithSourceVC:self apmentID:tagg Type:XVCExhi];
    
}

-(void)initData
{
    [self.view startLoading];
    [self getData];
}

-(void)refreshLayout:(CGRect)frame
{
    if (isHaveLookedUser)
    {
        self.showSeeUser.frame = CGRectMake(0,
                                            0,
                                            frame.size.width,
                                            [XVVNewUserSeeButton heightForNewUserSeeButton]);
        _showSeeUser.hidden = NO;
        self.tableview.frame = CGRectMake(0, _showSeeUser.frame.size.height+_showSeeUser.frame.origin.y
                                          , frame.size.width
                                          , frame.size.height-_showSeeUser.frame.size.height);
    }
    else
    {
        self.showSeeUser.hidden = YES;
        self.tableview.frame = CGRectMake(0
                                          , 0
                                          , frame.size.width
                                          , frame.size.height);

    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_tableview.mj_header beginRefreshing];
}

-(void)getData
{
    if(_isYuyueMessage){
        [XMLMessageModel requestMessageListWithAcceptType:@"0"
                                                listCount:0
                                               completion:^(BOOL isSuccess, NSMutableArray<XMLMessageModel *> *dataList,NSInteger lookCount) {
                                                   if (isSuccess)
                                                   {
                                                       if (lookCount>0)
                                                       {
                                                           isHaveLookedUser = YES;
                                                       }
                                                       else
                                                       {
                                                           isHaveLookedUser = NO;
                                                       }
                                                       
                                                       [self refreshLayout];
                                                       
                                                       useData = dataList;
                                                       
                                                       [_tableview reloadData];
                                                       
                                                   }
                                                   [self.view stopLoading];
                                                   [_tableview.mj_header endRefreshing];
                                               }];
    }else{
        [XMLMessageModel requestMessageLiuyanListWithAcceptType:0 completion:^(BOOL isSuccess,NSMutableArray<XMLMessageModel *>* dataList){
            if (isSuccess)
            {
                
                isHaveLookedUser = NO;
                [self refreshLayout];
                useData = dataList;
                
                [_tableview reloadData];
            }
            [self.view stopLoading];
            [_tableview.mj_header endRefreshing];
        }];
    }
    
}

-(void)footerFresh
{
    if(_isYuyueMessage){
        [XMLMessageModel requestMessageListWithAcceptType:@"0"
                                                listCount:useData.count
                                               completion:^(BOOL isSuccess, NSMutableArray<XMLMessageModel *> *dataList, NSInteger lookCount) {
                                                   if (isSuccess)
                                                   {
                                                       if (dataList.count==0)
                                                       {
                                                           [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无更多消息", nil)];
                                                       }
                                                       else
                                                       {
                                                           [useData addObjectsFromArray:dataList];
                                                           [_tableview reloadData];
                                                       }
                                                   }
                                                   [_tableview.mj_footer endRefreshing];
                                               }];
    }else{
        [XMLMessageModel requestMessageLiuyanListWithAcceptType:useData.count completion:^(BOOL isSuccess,NSMutableArray<XMLMessageModel *>* dataList){
            if (isSuccess)
            {
                
                isHaveLookedUser = NO;
                [self refreshLayout];
                useData = dataList;
                
                [_tableview reloadData];
            }
            [self.view stopLoading];
            [_tableview.mj_header endRefreshing];
        }];
    }
    
}

/**
 * 点击 显示 留言
 */
-(void)clickMessage{
    _isYuyueMessage=false;
    [self changeStyle];
    [self getData];
}

/**
 *  点击 显示 预约
 */
-(void)clickYuyueMessage{
    _isYuyueMessage=true;
    [self changeStyle];
    [self getData];
}

-(void)changeStyle{
    if(_isYuyueMessage){
        
        [_Liuyantemp setBackgroundColor:[UIColor whiteColor].CGColor];
        _message.backgroundColor=[XXTool colorWithProject];
        [_message setTitleColor:[XXTool colorWithProject] forState:UIControlStateNormal];
        // 预约
        [_temp setBackgroundColor:[XXTool colorWithProject].CGColor];
        _YuyueMessage.backgroundColor=[XXTool colorWithProject];
        [_YuyueMessage setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        [_Liuyantemp setBackgroundColor:[XXTool colorWithProject].CGColor];
        _message.backgroundColor=[XXTool colorWithProject];
        [_message setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [_temp setBackgroundColor:[UIColor whiteColor].CGColor];
        _YuyueMessage.backgroundColor=[XXTool colorWithProject];
        [_YuyueMessage setTitleColor:[XXTool colorWithProject] forState:UIControlStateNormal];
    }
}


#pragma mark tableviewDataSource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!_isYuyueMessage){
        VisitorMessageLiuyanCell *cell = [VisitorMessageLiuyanCell cellSelectionStyleNoneWithTableView:tableView];
        
        [cell setExhiCellWithData:useData[indexPath.row]];
        return cell;
    }else{
        XTCAppointmentInformationShow *cell = [XTCAppointmentInformationShow cellSelectionStyleNoneWithTableView:tableView];
        [cell setUseType:XTCAppointmentInformationShowMessage];
        [cell setCellWithData:useData[indexPath.row]];
        cell.delegate = self;
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [XTCAppointmentInformationShow heightForCell];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return useData.count;
}

#pragma mark XTCAppointmentInformationShowDelegate
-(void)appointmentInformationShowClickAgreeButtonWithCell:(XTCAppointmentInformationShow *)cell
{
    NSIndexPath *tempIndexPath = [_tableview indexPathForCell:cell];
    XMLMessageModel *temp = useData[tempIndexPath.row];
    [XVCAppontmentDetail pushAppontmentDetailVCWithSourceVC:self apPersonID:temp.orderid completion:^{
        [_tableview.mj_header beginRefreshing];
    }];
}

-(void)appointmentInformationShowClickCancelButtonWithCell:(XTCAppointmentInformationShow *)cell
{
    NSIndexPath *tempIndexPath = [_tableview indexPathForCell:cell];
    XMLMessageModel *temp = useData[tempIndexPath.row];
    [XVCRefuseVisitor pushRefuseVisitorVCWithSourceVC:self apPersonID:temp.orderid
                                           completion:^{
                                               [self.navigationController popViewControllerAnimated:YES];
                                           }];
}

#pragma mark tableviewdelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMLMessageModel *temp = useData[indexPath.row];
    if(_isYuyueMessage){
        
        [XVCAppontmentDetail pushAppontmentDetailVCWithSourceVC:self apPersonID:temp.orderid completion:^{
            [_tableview.mj_header beginRefreshing];
        }];
    }else{
        [XVCAppontmentDetail pushLiuyanDetailVCWithSourceVC:self apPersonID:temp.orderid];
    }
    
}

-(void)clickApponintmentRecord
{
    [XVCAppointmentRecord pushAppointmentRecordVCWithSourceVC:self];
}

-(void)clickShowSeenU
{
    [XVCSomeoneSeenU pushSomeSeenUVCWithSourceVC:self];
}

-(void)clickleft
{
    //[self.navigationController.sideViewController showLeftView];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
